package editors.objectedit;

import graphics.system.Color;

import java.util.Map;
import java.util.TreeMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import tools.Help;
import tools.Point;

public class FileObject {

	
	private TreeMap<String,String> attributes;
	private String objectName;
	
	public FileObject(String name){
		objectName = name;
		attributes = new TreeMap<String,String>();
	}
	
	public FileObject(Element e){
		objectName = e.getNodeName();
		NamedNodeMap nmap = e.getAttributes();
		for(int i=0;i<nmap.getLength();i++){
			attributes.put(nmap.item(i).getNodeName(),nmap.item(i).getNodeValue());
		}
		
	}
	
	public void setAttribute(String name,int i){
		attributes.put(name, String.valueOf(i));
	}
	public void setAttribute(String name,float f){
		attributes.put(name, String.valueOf(f));
	}
	public void setAttribute(String name,Point p){
		attributes.put(name, String.valueOf(p.toFileString()));
	}
	public void setAttribute(String name, Color c){
		attributes.put(name, c.toFileString());
	}
	
	private String getAttribute(String name){
		String attr = attributes.get(name);
		if(attr == null){
			Help.p("Error: Not such attribute: "+name);
		}
		return attr;
	}
	public float getAttributeFloat(String name){
		return Float.valueOf(getAttribute(name));
	}
	public int getAttributeInteger(String name){
		return Integer.valueOf(getAttribute(name));
	}
	public Point getAttributePoint(String name){
		return Point.valueOf(getAttribute(name));
	}
	
	
	public Element toXML(Document doc){
		Element e = doc.createElement(this.objectName);
		for(Map.Entry<String, String> attr : attributes.entrySet() ){
			e.setAttribute(attr.getKey(), attr.getValue());
		}
		return e;
	}
}
